package com.example.demoexample.activity;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.example.demoexample.R;
import com.example.demoexample.adapter.LooperAdapter;
import com.example.demoexample.view.MyLooperViewPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RoundPhotoActivity extends AppCompatActivity implements MyLooperViewPager.OnLooperViewpagerListener, ViewPager.OnPageChangeListener {
    private List<Integer> mPics = new ArrayList<>();
    private Handler mHandler;
    private MyLooperViewPager mMViewPager;
    private boolean isTouch = false;
    private LinearLayout mPointContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_round_photo);

        mMViewPager = findViewById(R.id.looperPager);
        mPointContainer = findViewById(R.id.pointContainer);
        LooperAdapter mLooperAdapter = new LooperAdapter();

        Random random = new Random();

        mPics.add(R.mipmap.one);
        mPics.add(R.mipmap.tow);
        mPics.add(R.mipmap.three);
        initPoint();
        mLooperAdapter.setData(mPics);
        mMViewPager.setAdapter(mLooperAdapter);
        setSelectPoint(0);
        mMViewPager.setCurrentItem(100 * mPics.size(),false);
        mHandler = new Handler();
        mMViewPager.setOnLooperViewpagerListener(this);
        mMViewPager.addOnPageChangeListener(this);
    }

    private void initPoint(){
        for (int i = 0;i<mPics.size();i++){
            View view = new View(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(40,40);
            layoutParams.leftMargin = 20;
            view.setBackgroundResource(R.drawable.shape_point_normal);
            view.setLayoutParams(layoutParams);
            mPointContainer.addView(view);
        }
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (!isTouch){
                int currenItem = mMViewPager.getCurrentItem();
                mMViewPager.setCurrentItem(++currenItem,true);
            }
            mHandler.postDelayed(this,3000);
        }
    };

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        mHandler.postDelayed(mRunnable,3000);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacks(mRunnable);
    }


    @Override
    public void onLooperTouch(boolean isTouch) {
        this.isTouch = isTouch;
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        int realPosition ;
        if (mPics.size()!=0){
             realPosition = i % mPics.size();
        }else {
            realPosition = 0;
        }
        
        setSelectPoint(realPosition);
    }

    private void setSelectPoint(int realPosition) {
        for (int i=0;i<mPointContainer.getChildCount();i++){
          View view =  mPointContainer.getChildAt(i);
            if (i != realPosition){
                view.setBackgroundResource(R.drawable.shape_point_normal);
            }else {
                view.setBackgroundResource(R.drawable.shape_point_select);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
